package com.homestay.service.impl;

import com.homestay.beans.BookInfo;
import com.homestay.beans.Books;
import com.homestay.dao.BooksDao;
import com.homestay.dao.HomeDao;
import com.homestay.dao.impl.BooksDaoImpl;
import com.homestay.dao.impl.HomeDaoImpl;
import com.homestay.service.BooksService;
import com.homestay.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class BooksServiceImpl implements BooksService {

    BooksDao booksDao = new BooksDaoImpl();
    HomeDao homeDao = new HomeDaoImpl();

    @Override
    public int addBooks(Books book) {
        JdbcUtils.getConnection();
        JdbcUtils.startTransaction();
        int count1 = 0;
        int count2 = 0;
        try {
            count1 = JdbcUtils.executeUpdate("INSERT INTO tb_books (vip_id, enterTime, exitTime, home_id, createTime) VALUES (?, ?, ?, ?, now())",
                    book.getVip_id(), book.getEnterTime(), book.getExitTime(), book.getHome_id());
            count2 = JdbcUtils.executeUpdate("UPDATE tb_home SET state = ? WHERE id = ?", "已入住", book.getHome_id());
            JdbcUtils.commit();
        } catch (Exception e) {
            JdbcUtils.rollback();
        } finally {
            JdbcUtils.close();
        }
        return count1 + count2 > 1 ? 1 : 0;
    }

    @Override
    public int deleteBooks(int id) {
        JdbcUtils.getConnection();
        JdbcUtils.startTransaction();
        int count1 = 0;
        int count2 = 0;
        try {
            int home_id = booksDao.getBooksById(id).getHome_id();
            count1 = JdbcUtils.executeUpdate("UPDATE tb_home SET state = ? WHERE id = ?",
                    "未打扫", home_id);
            count2 = JdbcUtils.executeUpdate("DELETE FROM tb_books WHERE id = ?", id);
           JdbcUtils.commit();
        } catch (Exception e) {
            JdbcUtils.rollback();
        } finally {
            JdbcUtils.close();
        }
        return count1 + count2 > 1 ? 1 : 0;
    }

    @Override
    public int updateBooks(Books book, int lastHomeId) {
        if(book.getHome_id() == lastHomeId) {
            // 如果房间没有变更，直接更新
            int count = booksDao.updateBooks(book);
            return count > 0 ? 1 : 0;
        }

        JdbcUtils.getConnection();
        JdbcUtils.startTransaction();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        try {
            count1 = JdbcUtils.executeUpdate("UPDATE tb_home SET state = ? WHERE id = ?", "空房", lastHomeId);
            count2 = JdbcUtils.executeUpdate("UPDATE tb_books SET vip_id = ?, enterTime = ?, exitTime = ?, home_id = ? WHERE id = ?",
                    book.getVip_id(), book.getEnterTime(), book.getExitTime(), book.getHome_id(), book.getId());
            count3 = JdbcUtils.executeUpdate("UPDATE tb_home SET state = ? WHERE id = ?", "已入住", book.getHome_id());
            JdbcUtils.commit();
        } catch (Exception e) {
            JdbcUtils.rollback();
        } finally {
            JdbcUtils.close();
        }

        return (count1 + count2+count3) > 2 ? 1 : 0;
    }

    @Override
    public List<Books> getBooks(String phone) {
        return booksDao.getBooks(phone);
    }

    @Override
    public Books getBooksById(int id) {
        return booksDao.getBooksById(id);
    }

    @Override
    public List<BookInfo> getBookInfo(String phone) {
        return booksDao.getBookInfo(phone);
    }

    @Override
    public BookInfo getBookInfoById(String id) {
        return booksDao.getBookInfoById(id);
    }
}
